#
# StatusDownloadProgressView.py
#
# This work is released under the GNU GPL, version 2 or later.
#
from Utils.kdeemul import *
from Utils.human import *
from StatusProgressBar import *
from Services.Downloader import *
from PixmapPushButton import *
from Utils.Paths import *
from Utils.utils import *

class StatusDownloadProgressView(QWidget):
	def __init__(self,container,statusView):
		global _backColor
		QWidget.__init__(self,container)
		self.statusView = statusView
		backColor=QColor(Settings.settings().get('ColorR'),Settings.settings().get('ColorG'),Settings.settings().get('ColorB'))
		self.setPaletteBackgroundColor(backColor)
		self.buildViews()
		QObject.connect(Downloader.singleton(),PYSIGNAL("status"),self.progress)
	
	def buildViews(self):
		#f = QFont("fixed",6)
		hlayout = QHBoxLayout(self)
		hlayout.addItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Fixed))
		vlayout = QVBoxLayout()
		vlayout.setSpacing(-5)
		hlayout.addLayout(vlayout,100)
		#vlayout.addItem(QSpacerItem(1,1,QSizePolicy.Fixed,QSizePolicy.Expanding))
		#self.title = QLabel(i18n("Song Name"),self)
		self.title = KSqueezedTextLabel(self)
		self.title.setText("Song Name")
		#self.title.setFont(f)
		self.title.setAlignment(Qt.AlignCenter)
		vlayout.addWidget(self.title)
		self.elapsed = QLabel(i18n("Completion"),self)
		#self.elapsed.setFont(f)
		self.elapsed.setAlignment(Qt.AlignCenter)
		vlayout.addWidget(self.elapsed)
		playout = QHBoxLayout()
		vlayout.addLayout(playout)
		playout.addItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Fixed))
		self.progressView = StatusProgressBar(self)
		playout.addWidget(self.progressView,100)
		playout.addItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Fixed))
		#vlayout.addItem(QSpacerItem(1,1,QSizePolicy.Fixed,QSizePolicy.Expanding))
		hlayout.addItem(QSpacerItem(1,1,QSizePolicy.Expanding,QSizePolicy.Fixed))
##		cancelIcon = QIconSet(QPixmap(art("buttonstatuscancel.png")))
##		self.cancelButton = SmallPixmapPushButton(cancelIcon,"",self)
##		QToolTip.add(self.cancelButton,i18n("Cancel the current rip"))
##		QObject.connect(self.cancelButton,SIGNAL("clicked()"),self.cancelRip)
##		hlayout.addWidget(self.cancelButton)
		
	def progress(self,status):
		if status['Source']=='Downloader':
			self.progressView.progress(status)
			state = status['Status']
			if state=='Begin':
				self.title.setText(i18n('Connecting'))
				self.elapsed.setText('')
				self.statusView.showStatus(self)
			elif state=='Downloading':
				self.title.setText(unikode(i18n("Downloading: %s (%d of %d)")) % (status['Track'],status.get('Index',0),status.get('Count',0)))
				percentage = long(100*status['currentTime']/status['totalTime'])
				self.elapsed.setText(unikode(i18n('Completion: %d%%')) % percentage)
			elif state=='End':
				self.statusView.hideStatus(self)
##
##	def cancelRip(self):
##		LMusicRipper.killCurrentRipper()
